<?php

namespace App\Http\Controllers\Order;

use App\Http\Controllers\Controller;
use App\Model\Order;
use App\Model\Parent_order;
use App\Model\User;
use Illuminate\Http\Request;

/**
 * Class OrderController
 * @package App\Http\Controllers\Order
 * @User wuqin
 * @Date 2018/4/13 0013
 * @Time 11:35
 */
class OrderController extends Controller
{
    /**
     * @var Parent_order
     */
    private $parent_order;
    /**
     * @var Order
     */
    private $order;

    /**
     * OrderController constructor.
     * @param Parent_order $parent_order
     * @param Order $order
     */
    function __construct(Parent_order $parent_order, Order $order)
    {
        $this->parent_order = $parent_order;
        $this->order = $order;
    }

    /**
     * 方法 : pay
     * @User wuqin
     * @DateTime 2018-04-13 11:35
     * @param Request $request
     * @param $parentOrderId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function pay(Request $request, $parentOrderId)
    {
        if (empty(intval($parentOrderId)))
            return $this->error_url("/", "非法请求", 3);

        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);
        $userModel = User::find($user["user_id"]);

        $this->parent_order = $this->parent_order->find((int)$parentOrderId);
        if (empty($this->parent_order))
            return $this->error_url("/", "非法订单", 3);

        if ($userModel->balance < $this->parent_order->all_price)
            return $this->error_url("/user/balance", "非法订单", 3);


        $data = array();

        $data["parent_order"] = $this->parent_order->toArray();
        $data["balance"] = $userModel->balance;

        return view("Order.pay", $data);
    }
}
